<?php

namespace App\Http\Controllers\Admin;

use App\Models\Common\CollArea;
use App\Models\Common\WebConfig;
use App\Models\Company\CompanyBasic;
use App\Models\PartTime\PartTimeJobs;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Style;


class CompanyPartTimeManageController extends CommonController
{

    /**
     * 审核职位
     * @param Request $request
     * @return mixed
     */
    public function auditPost(Request $request)
    {
        $input = $request->only('keyword', 'type');
        $type = trim($request->type);
        $keyword = trim($request->keyword);
        $partTimeJobs = DB::table('part_time_jobs as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->select(
                't1.id', 't1.title', 't1.updated_at', 't1.salary_price', 't1.salary_unit', 't1.salary_method', 't1.city_id', 't2.company_name', 't1.uid'
            )
            ->where(function ($query) use ($keyword) {
                if (strlen($keyword) > 0) {
                    $query->where('t1.title', 'like', '%' . $keyword . '%')
                        ->orWhere('t2.company_name', 'like', '%' . $keyword . '%');
                }
            })
            ->where(function ($query) use ($type) {
                if (strlen($type) > 0) {
                    $query->where('t1.job_status', $type);
                }
            })
            ->orderBy('t1.created_at', 'desc')
            ->paginate(20);
        $partTimeJobs->transform(function ($v) {
            return new PartTimeJobs(get_object_vars($v));
        });
        //分页分配参数 过滤boolean假值
        $appends = arrayFilter($input);
        //工地地区
        return Style::view('admin.part_time_manage.audit_post', compact('partTimeJobs', 'appends', 'type'));
    }

    /**
     * 管理预览兼职详情
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show(Request $request)
    {
        //查询省市区信息
        $areaList = CollArea::getComplete();
        $id = $request->id;
        //查询信息
        $info = PartTimeJobs::where('id', '=', $id)
            ->whereIn('job_status', [0, 1, 2])
            ->firstOrFail();
        //app端信息
        $app['person_ios'] = WebConfig::getConfig('appPersonHasIOS');
        $app['person_android'] = WebConfig::getConfig('appPersonHasAndroid');
        $app['company_ios'] = WebConfig::getConfig('appCompanyHasIOS');
        $app['company_android'] = WebConfig::getConfig('appCompanyHasAndroid');
        //查询当前所属省市区信息
        $nowArea = CollArea::getAreaAddress($info->city_id);
        $companyInfo = CompanyBasic::where('id', $info->com_id)->first(['profile']);
        return Style::view('part_time.index.show', compact('info', 'companyInfo', 'areaList', 'nowArea', 'app'));
    }

    /**
     * 通过审核
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postApproved(Request $request)
    {
        $partTimeId = $request->input('selected_id.*');
        foreach ($partTimeId as $k => $v) {
            PartTimeJobs::where('id', $v)->update([
                'job_status' => 1,
//                'updated_at' => date('Y-m-d H:i:s', time())
            ]);
        }
        flash('审核成功', 'success');
        return redirect()->back();
    }

    /**
     * 删除职位
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function postDel(Request $request)
    {
        $result = PartTimeJobs::where('id', $request->id)->update([
            'job_status' => 4,
//            'updated_at' => date('Y-m-d H:i:s', time())
        ]);
        if ($result) {
            return response()->json(['status' => 0, 'msg' => '职位删除成功！']);
        } else {
            return response()->json(['status' => 1, 'msg' => '职位删除失败，请稍后重试！']);
        }
    }

    /**
     * 批量删除职位
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postBatchDel(Request $request)
    {
        $partTimeId = $request->input('selected_id.*');
        foreach ($partTimeId as $k => $v) {
            PartTimeJobs::where('id', $v)->update([
                'job_status' => 4,
//                'updated_at' => date('Y-m-d H:i:s', time())
            ]);
        }
        flash('批量删除职位成功！', 'success');
        return redirect()->back();
    }
}

























